package com.education.platform.api.controller.paper;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.service.ExaminPaperService;
import com.education.platform.api.service.QuestionBankService;
import com.education.platform.api.service.SysDictionaryService;
import com.education.platform.api.vo.request.BuyPaperVO;
import com.education.platform.api.vo.request.CommitTestResultVO;
import com.education.platform.api.vo.request.ExaminPaperQueryVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.enums.QuestionTypeEnum;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.QuestionBankDO;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.education.platform.dao.model.ExaminPaperModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* describe: 试卷
* @author :
* @date: 2020-07-15 08:43:11.650
**/
@RestController
@Api("试卷")
@RequestMapping("examinPaper")
@Validated
public class ExaminPaperController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExaminPaperController.class);

	@Autowired
    private ExaminPaperService examinPaperService;
    @Autowired
    private QuestionBankService questionBankService;
    @Autowired
    private SysDictionaryService sysDictionaryService;

    /**
     * 分页查询
     * @param examinPaperQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询试卷")
    @PostMapping("getPageList")
    public ExaminPaperPageInfoVO selectPageList(@RequestBody @Valid ExaminPaperQueryVO examinPaperQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", examinPaperQueryVO.toString());
        LOGGER.info(info);

        //21-2该接口固定获取50条记录
        examinPaperQueryVO.setPageSize(50);

        Page<ExaminPaperResponseVO> page = PageHelper.startPage(examinPaperQueryVO.getPageNum(), examinPaperQueryVO.getPageSize());
        Integer uk = null;
        if(StringUtils.isNotEmpty(ThreadUser.getUserTokenRedisKey())){
            uk = ThreadUser.getUserId();
        }
        List<ExaminPaperModel> examinPaperList = examinPaperService.getPaperList(uk,examinPaperQueryVO.getGradeCode());
        ExaminPaperPageInfoVO examinPaperPageInfo = new ExaminPaperPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), examinPaperPageInfo);
        List<ExaminPaperResponseVO> voList = BeanUtil.convert(examinPaperList, ExaminPaperResponseVO.class);
        examinPaperPageInfo.setList(voList);
        return examinPaperPageInfo;
    }

    @ApiOperation(value = "获取试卷类型")
    @PostMapping("selectPaperType")
    public List<SysDictionaryResponseVO> selectGrade() {
        List<String> typeCodes = Arrays.asList(DicTypeEnum.PAPER_TYPE.getValue());
        List<SysDictionaryDO> dictionaryDTOS = sysDictionaryService.getListByDicTypeCodeList(typeCodes);
        return   BeanUtil.convert(dictionaryDTOS, SysDictionaryResponseVO.class);
    }


    /**
     * 提交订单
     * @param buyPaperVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "购买试卷")
    @PostMapping("buyExamPaper")
    public Boolean buyExamPaper(@RequestBody @Valid BuyPaperVO buyPaperVO) {
        return examinPaperService.buyExamPaper(buyPaperVO);
    }


    /**
     * 挑战答题题目
     *
     * @param
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @GetMapping("getTestPaperDetail")
    public List<PaperDetailResponseVO> getTestPaperDetail(@RequestParam Integer paperId) {
        List<QuestionBankDO> list = questionBankService.getQuestionsByPaperId(paperId);
        List<PaperDetailResponseVO> voList = new ArrayList<>();
        list.forEach(m -> {
            PaperDetailResponseVO vo = BeanUtil.convert(m, PaperDetailResponseVO.class);
            if(m.getType() == QuestionTypeEnum.SIMPLE.getValue()){
                //简单题 没有选项，有答案
                vo.setChoiceAnswer(Arrays.asList(m.getChoiceAnswer()));
            }else{
                //非简答题  单选，多选，判断  有选项和答案，选择需要解析成数组，答案需要解析成数组
                vo.setOptions(Arrays.asList(m.getOptionA().split("==")));
                vo.setChoiceAnswer(Arrays.asList(m.getChoiceAnswer().split("==")));
            }
            voList.add(vo);
        });

        return voList;
    }



    /**
     * 提交试卷成绩
     *
     * @param
     * @return
     */
    @ApiOperation(value = "提交试卷成绩")
    @PostMapping("commitTestResult")
    public ExamResultVO commitTestResult(@RequestBody @Valid CommitTestResultVO vo) {
        ExamResultVO examResultVO = new ExamResultVO();
        examResultVO.setProportion(examinPaperService.commitTestResult(vo.getPaperId(), vo.getScore()));
        return examResultVO;
    }

}
